package com.itheima.service.impl;

import com.itheima.mapper.BrandMapper;
import com.itheima.pojo.Brand;
import com.itheima.pojo.pageBean;
import com.itheima.service.BrandService;
import com.itheima.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class BrandServiceImpl implements BrandService {
    //1.创建SqlSessionFactory 工厂对象
    SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();




    @Override
    public List<Brand> selectAll() {
        //2.获取sqlsession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //3.获取BrandMapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        //4.调用方法
        List<Brand> brands = mapper.selectAll();


        //5.释放资源
        sqlSession.close();


        return brands;
    }

    @Override
    public void add(Brand brand) {
        //2.获取sqlsession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //3.获取BrandMapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        //4.调用add方法进行添加数据
        mapper.add(brand);

        //5.提交事务
        sqlSession.commit();

        //6.释放资源
        sqlSession.close();
    }

    @Override
    public void deleteByIds(int[] ids) {
        //2.获取sqlsession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //3.获取BrandMapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        mapper.deleteByIds(ids);

        sqlSession.commit();

        sqlSession.close();
    }

    @Override
    public pageBean<Brand> selectByPage(int currentPage, int pagesize) {
        //2.获取sqlsession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //3.获取BrandMapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        //4.计算开始索引
        int begin = (currentPage - 1) * pagesize;
        //计算查询条目数
        int size = pagesize;

        //查询当前页数据
        List<Brand> rows = mapper.selectByPage(begin, size);

        //查询总记录数
        int count = mapper.selectTotalCount();



        pageBean<Brand> pageBean = new pageBean<>();
        pageBean.setRows(rows);
        pageBean.setTotalcount(count);
        sqlSession.close();
        return pageBean;


    }

    @Override
    public pageBean<Brand> selectByPageAndCondition(int currentPage, int pagesize, Brand brand) {
        //2.获取sqlsession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //3.获取BrandMapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        //4.计算开始索引
        int begin = (currentPage - 1) * pagesize;
        //计算查询条目数
        int size = pagesize;
        //处理brand数据，模糊查询
        String brandName = brand.getBrandName();
        if (brandName != null && brandName.length()>0){
            brand.setBrandName("%"+brandName+"%");
        }
        String companyName = brand.getCompanyName();
        if (companyName != null && companyName.length()>0){
            brand.setCompanyName("%"+companyName+"%");
        }


        //查询当前页数据
        List<Brand> rows = mapper.selectByPageAndCondition(begin, size,brand);

        //查询总记录数
        int count = mapper.selectTotalCountBycondition(brand);
        System.out.println(count);



        pageBean<Brand> pageBean = new pageBean<>();
        pageBean.setRows(rows);
        pageBean.setTotalcount(count);
        sqlSession.close();
        return pageBean;
    }


}
